import json
import os

files = [
    "alpacaeval.jsonl",
    "advbench.jsonl",
    "osum-commoneval-test-audio.jsonl",
    "ifeval.jsonl",
    "openbookqa.jsonl",
    "sd-qa.jsonl",
    "mmsu_biology.jsonl",
    "mmsu_business.jsonl",
    "mmsu_chemistry.jsonl",
    "mmsu_economics.jsonl",
    "mmsu_engineering.jsonl",
    "mmsu_health.jsonl",
    "mmsu_history.jsonl",
    "mmsu_law.jsonl",
    "mmsu_other.jsonl",
    "mmsu_philosophy.jsonl",
    "mmsu_physics.jsonl",
    "mmsu_psychology.jsonl"
]

src_root = "/mnt/sfs/asr/code/osum_xlgeng_3B/examples/wenetspeech/whisper/exp/full_train_llm_3B_epoch_0/test_step_37499.pt_only_X"
tgt_root = "/mnt/sfs/asr/code/osum_xlgeng_3B/voicebench_wsy/data"
output_root = "/mnt/sfs/asr/code/osum_xlgeng_3B/voicebench_wsy/0_37499_s2t"

os.makedirs(output_root, exist_ok=True)

for fname in files:
    src_result_path = os.path.join(src_root, fname.replace(".jsonl", ""), "llmasr_decode", "result.jsonl")
    tgt_jsonl_path = os.path.join(tgt_root, fname)
    output_path = os.path.join(output_root, fname)

    if not os.path.exists(src_result_path):
        print(f"跳过 {src_result_path}，文件不存在")
        continue
    if not os.path.exists(tgt_jsonl_path):
        print(f"跳过 {tgt_jsonl_path}，文件不存在")
        continue

    # 读取 result.jsonl
    responses = []
    with open(src_result_path, "r", encoding="utf-8") as fin:
        for line in fin:
            obj = json.loads(line)
            responses.append(obj["response"])

    # 读取目标 jsonl，按行合并 response
    merged_lines = []
    with open(tgt_jsonl_path, "r", encoding="utf-8") as fin:
        for idx, line in enumerate(fin):
            obj = json.loads(line)
            if idx < len(responses):
                obj["response"] = responses[idx]
            merged_lines.append(json.dumps(obj, ensure_ascii=False))

    # 保存到新文件
    with open(output_path, "w", encoding="utf-8") as fout:
        for line in merged_lines:
            fout.write(line + "\n")

    print(f"{fname} 合并完成，输出到 {output_path}")